*===============================================================================
* Project title		Savings Accounts to Borrow Less	
* PIs			Felipe Kast, Dina Pomeranz 
* File name		dataCreation.do
*-------------------------------------------------------------------------------
* Description		Creates final dataset used for all subsequent analyses  
* Outputs		impactDatabase.dta
* Spaces per tab 	8 
*===============================================================================

*===============================================================================
* START
*===============================================================================

*===============================================================================
* I. Preamble
*===============================================================================
*Section II     - Merge all data sources
*Section III    - Input data and clean
*Section IV     - Create key variables
*Section V      - Winsorization
*Section VI     - Final data
*-------------------------------------------------------------------------------
* I.1. Set-up
*-------------------------------------------------------------------------------
*Define path
clear all
set more off
global path "~/file_server/project_savings/2_shared/impact/do/November2019"	// Working directory 
global inputPath "$path/datasets"						// Path to processed data 
global log "$path/logs"

*Install winsor command 
ssc install winsor
	
*===============================================================================
* II. Merge all data sources 
*===============================================================================
*-------------------------------------------------------------------------------
* II.1. Imports id key that allows us to match observations from the baseline
* 	survey with the follow-up survey
*-------------------------------------------------------------------------------
* Original file is in Excel format
	use $inputPath/folio2009_2008.dta, clear
	
	rename Folio2009  idFollowUpSurvey
	rename folio12008 idBaseSurvey
	
	// Note: the following variables are the panel IDs marking individuals 
	keep idFollowUpSurvey idBaseSurvey 
	save $inputPath/tempKeyId.dta, replace					
*-------------------------------------------------------------------------------
* II.2. Imports FE/Banco de Chile bank account records and transactions
*-------------------------------------------------------------------------------
* Original file is in Excel format. For some reason, column names do not match
* the data column content. Proceed with caution and double check. We will 
* rename the relevant columns from the original file to their actual function

	use $inputPath/Consulta_Ahorro_2010.dta, clear					
										    
	// Rename & relabel the relevant columns to names denoting their function correctly. 
	rename (folio         STATUS_MOV FECHA_MOV FECPROC SUC_ORIGEN) ///
	       (idBaseSurvey  dateT      typeT     amountT nameTypeT)
		
	label variable idBaseSurvey "ID from baseline survey"
	label variable dateT        "Date of transaction"	 
	label variable typeT        "Type of transaction"
	label variable amountT      "Amount of transaction"
	label variable nameTypeT    "Name of transaction type"
	
	sort idBaseSurvey dateT

* II.2.a. Computes dummy indicating if subject used the account at least once --
* Take up is defined as depositing money at least once during the entire period
	
	// Types of deposit are indicated with the following values: 15, 200, 210,
	// 310 and 2111
	gen t1 = (typeT == 15  | typeT == 200 | typeT == 210 | ///	
		  typeT == 310 | typeT == 2111)

	egen openedAccount = max(t1), by(idBaseSurvey)			


* II.2.b. Computes total deposits of Jul-08-Aug-09 -----------------------------
	
	// computes total deposits of the period Jul-08-Aug-09
	egen t4 = total(amountT) if dateT >= 20080701 & ///		
		                    dateT <= 20090731 & ///
				    (typeT == 15 | typeT == 200 | typeT == 210 | typeT == 310 | typeT == 2111), ///
				    by(idBaseSurvey)
	
	replace t4 = 0 if t4 == . 
	
	egen totalDepositedJul08Aug09 = max(t4), by(idBaseSurvey)	

	// computes total deposits of the period start-Aug-09
	egen t_4 = total(amountT) if dateT >= 20080501 & ///
		                     dateT <= 20090731 & ///
				     (typeT == 15 | typeT == 200 | typeT == 210 | typeT == 310 | typeT == 2111), ///
				     by(idBaseSurvey)
	
	replace t_4 = 0 if t_4 == . 
	
	egen totalDepositedstartAug09 = max(t_4), by(idBaseSurvey)		


// Monthly deposit variable Jul - Dec (2008)
    foreach mon in 07 08 09 10 11 12 {

	egen t4_m`mon'_y8 = total(amountT) if dateT >= 2008`mon'01 & ///
		                          dateT <= 2008`mon'31 & ///
					  (typeT == 15 | typeT == 200 | typeT == 210 | typeT == 310 | typeT == 2111), ///
					  by(idBaseSurvey)
	
	replace  t4_m`mon'_y8 = 0 if t4_m`mon'_y8  == . 
	
	egen tDm`mon'y2008= max( t4_m`mon'_y8 ), by(idBaseSurvey)
    }

// Monthly deposit variable Jan - August (Up until August) 2009.
    foreach mon in 01 02 03 04 05 06 07 08 {

    egen t4_m`mon'_y9 = total(amountT) if dateT >= 2009`mon'01 & ///
		                          dateT <= 2009`mon'31 & ///
					  (typeT == 15 | typeT == 200 | typeT == 210 | typeT == 310 | typeT == 2111), ///
					  by(idBaseSurvey)
	
	replace  t4_m`mon'_y9 = 0 if t4_m`mon'_y9  == . 
	
	egen tDm`mon'y2009= max( t4_m`mon'_y9 ), by(idBaseSurvey)
    }

	
* II.2.c. Computes # of deposits and # of withdrawals --------------------------
	
	// # of deposits, Jul08-Aug09
	egen t5 = count(amountT) if (dateT >= 20080701 & dateT <= 20090731) & ///
		                    (typeT == 15 | typeT == 200 | typeT == 210 | typeT == 310 | typeT == 2111), ///
				    by(idBaseSurvey)
		
	replace t5 = 0 if t5 == . 
	egen numDepositsJul08Aug09 = max(t5), by(idBaseSurvey)
	
	// # of withdrawals, Jul08-Aug09
	// Types of withdrawals are indicated with the following values: 510 and 5111
	egen t7 = count(amountT) if (dateT >= 20080701 & dateT <= 20090731) & ///
		                    (typeT == 510 | typeT == 5111), ///
				    by(idBaseSurvey)
	
	replace t7 = 0 if t7 == . 
	egen numWithdrawalsJul08Aug09 = max(t7), by(idBaseSurvey)

* II.2.d. Computes total withdrawals of Jul-08-Aug-09 --------------------------
	
	// computes total withdrawal of the period Jul-08-Aug-09. 
	egen t8 = total(amountT) if dateT >= 20080701 & dateT <= 20090731 & ///
		                    (typeT == 510 | typeT == 5111), ///
				    by(idBaseSurvey)
	
	replace t8 = 0 if t8 == . 
	egen totalWithdrawalJul08Aug09 = max(t8), by(idBaseSurvey)
	
	// computes total withdrawal of the period start-Aug-09. 
	egen t_8 = total(amountT) if  dateT>= 20080501 & dateT <= 20090731 & ///
		                      (typeT == 510 | typeT == 5111), ///
				      by(idBaseSurvey)
	
	replace t_8 = 0 if t_8 == . 
	egen totalWithdrawalstartAug09 = max(t_8), by(idBaseSurvey)
	

// Monthly withdrawal variable Jul - Dec (2008)
    foreach mon in 07 08 09 10 11 12 {

	egen t8_m`mon'_y8 = total(amountT) if dateT >= 2008`mon'01 & dateT <= 2008`mon'31 & ///
					      (typeT == 510 | typeT == 5111), ///
					      by(idBaseSurvey)

	replace  t8_m`mon'_y8= 0 if t8_m`mon'_y8 == . 

	egen tWm`mon'y2008= max( t8_m`mon'_y8 ), by(idBaseSurvey)
    }


// Monthly withdrawal variable Jan - July (Up until August) 2009. 
    foreach mon in 01 02 03 04 05 06 07 08 {

    egen t8_m`mon'_y9 = total(amountT) if dateT >= 2009`mon'01 & dateT <= 2009`mon'31 & ///
                                          (typeT == 510 | typeT == 5111), ///
					  by(idBaseSurvey)
	
	replace  t8_m`mon'_y9= 0 if t8_m`mon'_y9 == . 
	
	egen tWm`mon'y2009= max( t8_m`mon'_y9 ), by(idBaseSurvey)
    }



* II.2.e. Computes average monthly net new savings -----------------------------
	// creates money flow variable. If positive, transaction added money to
	// the savings account. If negative, it is a withdrawal. Data is from excel
	// file, sheet "Hoja3". Also can be inferred by looking at a table 
	// generated by command "tabulate typeT nameTypeT"
	// Starting from August because that is the first month when all 
	// participants already are treated from the start of the month.
	// Some began treatment in late June, others in one of the first two weeks 
	// of July. Therefore the first month in which all participants have the 
	// treatment from the first calendar day on is August.
	
	//deposits
	gen flow     =      amountT if typeT == 15  | typeT == 200 | typeT == 210 | ///
	                               typeT == 310 | typeT == 2111
	//withdrawals
	replace flow = -1 * amountT if typeT == 510 | typeT == 5111
	
	local j = 1
	// Iterate over data collection points to sum up flows
	foreach t in 20080831 20080931 20081031 20081131 20081231 20090131 	///
		     20090231 20090331 20090431 20090531 20090631 20090731 	///
		     20090831 20090931 20091031 {	

		egen ta`j' = total(flow) if dateT <= `t', by(idBaseSurvey)
		egen tb`j' = max(ta`j'), by(idBaseSurvey)
		replace tb`j' = 0 if tb`j' == . 
		
		local j = `j' + 1
	}
	
	// computes monthly average
	egen avgMonthSavingsAug08Aug09 = rowmean(tb1 tb2 tb3  tb4  tb5 tb6 tb7 	///
	                                         tb8 tb9 tb10 tb11 tb12)		
	// computes monthly standard deviation
	egen sdMonthSavingsAug08Aug09  = rowsd(  tb1 tb2 tb3  tb4  tb5 tb6 tb7  ///
	                                         tb8 tb9 tb10 tb11 tb12)
		
* II.2.f. Creates active user dummy --------------------------------------------
	// Note: User defined as active if largest single deposit over 1000 Peso
	// computes total deposits until Aug-09. 
	
	egen t41 = total(amountT) if dateT >= 20080501 & dateT <= 20090731 & ///
	                             (typeT == 15 | typeT == 200 | typeT == 210 | typeT == 310 | typeT == 2111), ///
				     by(idBaseSurvey)
	replace t41 = 0 if t41 == . 
	egen totalDepositedAug09 = max(t41), by(idBaseSurvey) 
	
	gen t9 = (totalDepositedAug09 > 1000)					
	egen activeUser = max(t9), by(idBaseSurvey) 		
			
* II.2.g. Activity in the last six months --------------------------------------

	/*Depending on the individual, the follow-up survey was completed on 
	either June or July of 2009. Thus, we have to include all of January 
	just in case to make this measure comparable to the question in the 
	followup survey asking whether the participant made a deposit or 
	withdrawal in the last six months.*/
	
	// computes total deposits of the period Jan-09-Aug-09.
	egen t42 = total(amountT) if dateT >= 20090101 & ///
		                     dateT <= 20090731 & ///
				     (typeT == 15 | typeT == 200 | typeT == 210 | typeT == 310 | typeT == 2111), ///
				     by(idBaseSurvey)
	replace t42 = 0 if t42 == . 
	egen totalDepositedJan09Aug09 = max(t42), by(idBaseSurvey)
	
	// computes total withdrawal of the period Jan-09-Aug-09. 
	egen t43 = total(amountT) if dateT >= 20090101 & ///
		                     dateT <= 20090731 & ///
				     (typeT == 510 | typeT == 5111), /// 
				     by(idBaseSurvey)
	replace t43 = 0 if t43 == . 
	egen totalWithdrawalJan09Aug09 = max(t43), by(idBaseSurvey)

* II.2.h. Clean, structures and saves temporary database -----------------------

	keep idBaseSurvey openedAccount activeUser numDepositsJul08Aug09 ///		
		totalDepositedJul08Aug09 numWithdrawalsJul08Aug09 ///
		totalWithdrawalJul08Aug09 avgMonthSavingsAug08Aug09 ///
		sdMonthSavingsAug08Aug09 totalDepositedJan09Aug09 ///
		totalWithdrawalJan09Aug09 totalDepositedstartAug09 ///
		totalWithdrawalstartAug09 tDm* tWm* 

			
	gen t2h = -1*totalWithdrawalstartAug09 if totalWithdrawalstartAug09 != .	        
	egen finalBalstartAug09 = rowtotal(totalDepositedstartAug09 t2h), missing
		
	gen Gr0Aug09 = (finalBalstartAug09!=. & finalBalstartAug09>0)                          
		
	// cleaning before saving
	duplicates drop // drops duplicates				
	keep if idBaseSurvey != . // drops unmergeable subjects
	
	save $inputPath/tempBankData.dta, replace
	
*-------------------------------------------------------------------------------
* II.3. Appends follow up surveys 
*-------------------------------------------------------------------------------

	local it =  0 // initialises a counter
	
	// Loop through datasets of follow-up data sets - each DTA-file represents
	// the data set from one particular treatment's survey
	foreach db in JPAA.dta JPCS.dta JPMA.dta OCAA.dta OCCS.dta OCMA.dta ///
		JPSS.dta OCSS.dta Base_controles.dta {
		
		use $inputPath/`db', clear
		gen source = "`db'" // creates group id auxiliary variable
		
		// replacements made to make variables consistent across surveys
		// e.g.: some surveys c3_1, others c3_01
		capture rename c3_1 c3_01
		capture rename c3_2 c3_02
		capture rename c3_3 c3_03
		capture rename c3_5 c3_05
		capture rename c3_6 c3_06
		capture rename c3_7 c3_07
		capture rename c3_8 c3_08
		capture rename c3_9 c3_09
		capture gen d12 = .
		capture gen savingDecision = .
		capture gen hideSavingsFamily = .
		capture gen hideSavingsSO = .
		capture gen inDicom = .
		
		if (strpos("`db'", "OC") != 0) { 
			replace savingDecision = d17
			replace hideSavingsFamily = d16
			replace hideSavingsSO = d15
			replace inDicom = d20
		} 
		else if (strpos("`db'", "JP") != 0) { 
			replace savingDecision = d8
			replace hideSavingsFamily = d7
			replace hideSavingsSO = d6
			replace inDicom = d5
		} 
		else {
			replace savingDecision = d6
			replace hideSavingsFamily = d5
			replace hideSavingsSO = d4
			replace inDicom = d9
		}
		
		// Restrict to most relevant survey items
		keep d1_am d1_bm d1_cm d1_em k1_dm k1_fm k1_hm k1_im k1_jm ///
			k1_lm k1_mm k1_nm k1_om k1_pm k1_qm k1_rm k1_tm  ///
			k1_wm folio source k1_em k1_d k1_f k1_h k1_i k1_j  ///
			k1_l k1_m k1_n k1_o k1_p k1_q k1_r k1_t k1_v ///
			k1_w k1_e k2_1m k2_2m k2_3m k2_4m k2_5m k2_6m k2_7m ///
			k2_8m k2_9m k2_1 k2_2 k2_3 k2_4 k2_5 k2_6 k2_7 ///
			k2_8 k2_9 d1_dm d1_fm d1_gm d1_hm d1_im d1_a d1_b ///
			d1_d d1_e d1_f d1_g d1_h d1_i c3_01 c3_02 c3_03 ///
			c3_05 c3_06 c3_07 c3_08 c3_09 c1_1_jun c1_1_may ///
			c1_7_jun c1_7_may c1_7_abr m3_1 c5 a_01 a_03_1 ///
			p_1 g4 a_05 k1_km c1_1_abr a_03_2 k1_vm k1_k d1_c ///
			k1_a k1_b k1_c k1_am k1_bm k1_cm d12 b_3 d3_a d3_b d3_c ///
			savingDecision hideSavingsFamily b_1  d2_a d2_b d2_c ///
			hideSavingsSO m1 m2 b_2_1 b_2_2 b_2_3 b_2_4 c6 a_07 ///
			a_03_1 a_03_2 a_03_3 a_03_4 m3_2 m3_4 m3_5 m3_6 inDicom e2 c1_1 ///
			a_03_1 a_03_2 a_03_3 a_03_4 m3_2 m3_4 m3_5 m3_6 inDicom e2 c1_1 ///
			c1_1_* c1_2_* c1_3_* c1_4_* c1_5_* c1_6_* c1_7_* c1_8_* c1_9_*
		
		rename folio idFollowUpSurvey

		if (`it' != 0) {
			append using $inputPath/tempFollowUpSurvey.dta 
		}
		save $inputPath/tempFollowUpSurvey.dta, replace
		
		local it = `it' + 1 // updates the counter
	}

*-------------------------------------------------------------------------------
* II.4. Merges baseline variables
*-------------------------------------------------------------------------------
	
	// merges follow-up survey with key id file.
	merge 1:1 idFollowUpSurvey using $inputPath/tempKeyId.dta, ///
		nogenerate 
	
	// merges baseline survey with working database
	rename idBaseSurvey folio1 // rename variable to match baseline survey
	*merge 1:1 folio1 using `path'/input/Encuesta_FE.dta, nogenerate 
	merge 1:1 folio1 using $inputPath/Encuesta_FE.dta, nogenerate 
	rename folio1 idBaseSurvey // rename again

*-------------------------------------------------------------------------------
* II.5. Merges bank account data
*-------------------------------------------------------------------------------
* Savings account data comes from Banco de Chile/Fondo Esperanza administrative
* bank data.
	
	merge 1:1 idBaseSurvey using $inputPath/tempBankData.dta, ///
		nogenerate l
		
*-------------------------------------------------------------------------------
* II.6. Merges with Treatment group assigment data (randomization)
*-------------------------------------------------------------------------------	
* This database indicates to which group each subject was randomly assigned

	rename idBaseSurvey folio1 // rename variable to match randomization data
	merge m:1 folio1 using $inputPath/070109_sample_RANDOMIZED.dta, ///
		nogenerate
	rename folio1 idBaseSurvey
	
*===============================================================================
* III. Inputs data
*===============================================================================
* First, we remove odd values (e.g. 9999999, which represents a codified missing). Then,
* we impute 0 balance when the subject stated it did not have that type of account.
*-------------------------------------------------------------------------------
* III.1. Cleans survey data on savings, debt, loans, and poverty events
*-------------------------------------------------------------------------------

//Define sets of variables to be cleaned
//Variables from the 2008 survey that represent amounts
local amVars08 	p071 p081m p072 p082m p073 p083m p074 p084m p075 p085m p076 p086m p077 p087m /* Savings 2008 */ ///
				p1601 p1701m p1602 p1702m p1603 p1703m p1604 p1704m p1605 p1705m p1606 p1706m ///
				p1607 p1707m p1608 p1708m p1609 p1709m p1610 p1710m p1611 p1711m p1612 p1712m ///
				p1613 p1713m p1614 p1714m p1615 p1715m p1616 p1716m p1617 p1717m p1618 p1718m ///
				p1619 p1719m p1620 p1720m p1621 p1721m p1622 p1722m p1623 p1723m /* Debt 2008 */ ///
				p1801 p1901m p1802 p1902m p1803 p1903m p1804 p1904m p1805 p1905m p1806 p1906m ///
				p1807 p1907m p1808 p1908m p1809 p1909m /* Loans 2008 */
//Variables from the 2009 survey that represent amounts
local amVars09 	d1_a d1_b d1_c d1_d d1_e d1_f d1_g d1_h d1_i /*Savings 2009*/ ///
				k1_a k1_b k1_c k1_d k1_e k1_f /*k1_g*/ k1_h k1_i k1_j k1_k k1_l k1_m k1_n ///
				k1_o k1_p k1_q k1_r /*k1_s*/ k1_t /*k1_u*/ k1_v k1_w /* Debt 2009 */ ///
				k2_1 k2_2 k2_3 k2_4 k2_5 k2_6 k2_7 k2_8 k2_9 /* Loans 2009 */
//Variables from both 2008 and 2009 representing binary questions corresponding to amount questions above				
local binVars	p15 /* Dicom Marker 2008 */ ///
				p1601 p1602 p1603 p1604 p1605 p1607 p1608 p1609 p1610 p1611 p1612 p1613 p1614 p1615 ///
				p1616 p1617 p1618 p1619 p1620 p1621 p1622 p1623/* Debt 2008 */ ///
				k1_a k1_b k1_c k1_d k1_e k1_f /*k1_g*/ k1_h k1_i k1_j k1_k k1_l k1_m k1_n k1_o k1_p k1_q ///
				k1_r /*k1_s*/ k1_t k1_v /*k1_u*/ k1_w /* Debt 2009 */ ///
				p1801 p1802 p1803 p1804 p1805 p1806 p1807 p1808 p1809 /* Loans 2008 */ ///
				k2_1 k2_2 k2_3 k2_4 k2_5 k2_6 k2_7 k2_8 k2_9 /* Loans 2009 */ ///
				p2301 p2302 p2303 p2305 p2306 p2307 p2308 p2309 /* Poverty Events 2008 */ ///
				c3_01 c3_02 c3_03 c3_05 c3_06 c3_07 c3_08 c3_09 /* Poverty Events 2009 */ ///
				p071 p072 p073 p074 p075 p076 p077 /* Savings 2008 */ ///
				d1_a d1_b d1_c d1_d d1_e d1_f d1_g d1_h d1_i /* Savings 2009 */ 
//Combined list of all variables
local allVars	`amVars08' `amVars09' `binVars' 
//Add 2009 survey amount variables to list of all variables
foreach x in `amVars09' {
	local allVars `allVars' `x'm
}		

//NOTE: What is the general encoding for the variables pre-cleaning:
// - 2008 survey, binary (e.g. p071):
//	contains extremely few missings; contains three values 1 == "si". 2 =="no", 9="no responde"
// - 2008 survey, amount (e.g. p081m): 
//	contains missings; contains "no responde" == 99999999; valid values divisible by 500 with few exceptions 

********************************************************************************
//NOTE: Steps of data cleaning for all variables:
// - remove unrealistically high values (>=10^7)
// - remove 99999/999999/9999999/99999999
// - set amount to zero if corresp. binary answer == "no"	
// - set amount to missing if corresp. binary answer marked as missing (== 9)
// do this for all relevant variables both 2008/2009 consistently (first amount then binary)
// create detailed missing report before&after cleaning
********************************************************************************

//Clean amount variables from 2008 survey
// Define iterators
local it = 0
local this_binvar = ""
// Loop through all 2008 amount variables
foreach var in `amVars08' {
	//Every second variable: keep the first variable of each pair as the "binvar" and proceed to clean the actual var (second member of pair)
	if `it' == 0 {		
		local this_binvar = "`var'"	
		local it = 1
	} 
	else {
		di "`var' `this_binvar' "	//Test display
		//a) set amount to missing where unrealistically high values (10^8 or larger)	
		replace `var' = . if `var' >= 9999999 //9 	
		//b) set to missing where amount equal to missing marker (incl. likely typos)
		replace `var'  = . if `var' == 99999 | `var' == 999999 | ///
			`var' == 9999999 | `var' == 99999999 			
		//c) set amount to missing where answer of binary question is missing or marked as missing
		replace `var'  = . if `this_binvar' == 9
		replace `var'  = . if `this_binvar' == .		
		//d) set amount to zero where answer of binary is "no".
		replace `var'  = 0 if `this_binvar' == 2
		//reset variable pair indicator
		local it = 0		
	}
}
//Clean amount variables from 2009 survey, new way
foreach var in `amVars09' {					
		//a) set amount to missing where unrealistically high values (10^8 or larger)	
		replace `var'm = . if `var'm >= 9999999 // 9 	
		//b) set to missing where amount equal to missing marker (incl. likely typos)
		replace `var'm  = . if `var'm == 99999 | `var'm == 999999 | ///
			`var'm == 9999999 | `var'm == 99999999 	| `var'm == 9999998 	
		//c) set amount to missing where answer of binary question is missing or marked as missing
		replace `var'm  = . if `var' == 9
		replace `var'm  = . if `var' == .		
		//d) set amount to zero where answer of binary is "no".
		replace `var'm  = 0 if `var' == 2
		}

// Recode binary answer variables to make counting across variables easier below
// a) Set missing-marker to actual missing value
// b) Set value to 0 if answer is "no"(==2)
//Iterate through all binary variables to be cleaned
foreach x in  `binVars' {	
		replace `x' = . if `x' == 9
		replace `x' = 0 if `x' == 2  		
	}

*-------------------------------------------------------------------------------
* III.2. Inputs missings and 0 for bank data
*-------------------------------------------------------------------------------	

	// Bank data only record subjects who opened accounts, so replacing 0
	// for non users is required
	replace openedAccount = 0 if openedAccount == .
	replace activeUser = 0 if activeUser == .

// Dropping 38 observations where no treatment was assigned 
	drop if treatment == .
*===============================================================================
* IV. Creates key variables
*===============================================================================
*-------------------------------------------------------------------------------
* IV.1. Creates treatment identifier variables
*-------------------------------------------------------------------------------
	gen accountAccess = (treatment > 0) if treatment != .
	gen peerGroupTreatment = (treatment == 3) if treatment != .
	gen highInterestTreatment = (treatment == 2) if treatment != .

*-------------------------------------------------------------------------------
* IV.2. Age
*-------------------------------------------------------------------------------
* Age is inferred from date of birth obtained FE administrative records.
	// creates age only for those older than 14 years old
	gen age = 2007 - round(fechanac / 10000) if fechanac < (2007-14)*10000

*-------------------------------------------------------------------------------
* IV.3. Years of Education
*-------------------------------------------------------------------------------
* Years of education is obtained from FE's administrative data (2008) 		
	gen educationYears = escolaridad if escolaridad != 99

*-------------------------------------------------------------------------------
* IV.4. Monthly Household's income per capita (2008)
*-------------------------------------------------------------------------------
* Monthly household's income per capita is calculated by dividing the household
* income by number of family members. Both variables are obtained from FE's 
* administrative data. We drop unrealistic values
	gen incomePerCapita = hogarini / integfamini if hogarini >= 25000 & ///
		hogarini <= 5000000

*-------------------------------------------------------------------------------
* IV.5. Household size in terms of number of members
*-------------------------------------------------------------------------------
* Household size is obtained from baseline survey. It comes from FE's 
* administrative data 
	gen householdSize2008 = integfamini 

*-------------------------------------------------------------------------------
* IV.6. Dummy indicating if subject has savings account prior to the study
*-------------------------------------------------------------------------------
* Whether subject has a previous saving account is obtained from baseline survey
	gen hasPrevAccount = (p071 == 1) if p071 != . // excludes missings
	gen noPrevAccount2008= (p071 == 0) if p071 != .
	
	gen noPrevAccountOnly2008=.
		replace noPrevAccountOnly2008=1 if p071==0
		replace noPrevAccountOnly2008=0 if p071==1
		
	gen noPrevAccountOnlyM2008=noPrevAccountOnly2008
		replace noPrevAccountOnly2008=1 if p071==.

*-------------------------------------------------------------------------------
* IV.7. Savings amount
*-------------------------------------------------------------------------------
* IV.7.a.i Formal Savings ------------------------------------------------------
	egen formalSavings2008 = rowtotal(p081m p083m), missing 			
	egen formalSavings2009 = rowtotal(d1_am d1_bm d1_cm d1_em), missing	

* IV.7.a.ii Formal Savings with FE admin data  --------------------------------
	egen formalSavingsAd2008=rowtotal(p081m p083m), missing
	egen formalSavingsAd2009=rowtotal (d1_cm d1_em finalBalstartAug09 ///
		d1_bm), missing
		
* IV.7.b.i All Savings ---------------------------------------------------------
	egen allSavings2008 = rowtotal(p081m p082m p083m p084m p085m p086m ///
		/*p086m*/ p087m), missing
	egen allSavings2009 = rowtotal(d1_am d1_bm d1_cm d1_dm d1_em d1_fm ///
		d1_gm d1_hm d1_im), missing

* IV.7.b.ii All Savings with FE admin data  -----------------------------------
	gen allSavingsAd2008 =allSavings2008
	egen allSavingsAd2009 = rowtotal( d1_cm d1_dm d1_em d1_fm d1_gm ///
		d1_hm d1_im finalBalstartAug09 d1_bm ), missing	
	
* IV.7.b.iii All Savings with FE admin data (coding negative as zero) ---------
	gen allSavingsAdPOS2008 =allSavings2008
	gen finalBalstartAug09_pos = finalBalstartAug09
	replace finalBalstartAug09_pos = 0 if finalBalstartAug09_pos < 0
	egen allSavingsAdPOS2009 = rowtotal( d1_cm d1_dm d1_em d1_fm d1_gm ///
		d1_hm d1_im finalBalstartAug09_pos d1_bm ), missing	
	
	
* IV.7.c Had Formal Savings in 2008 --------------------------------------------
	gen hadFormalSavings2008 = (formalSavings2008 > 0) if ///
		formalSavings2008 != .
	gen noFormalSavings2008=.
		replace noFormalSavings2008=1 if formalSavings2008==0
		replace noFormalSavings2008=0 if formalSavings2008 > 0 & ///
		formalSavings2008!=.

* IV.7.d.i Individual categories (indexes for savings) -------------------------
	egen savingsCat2008 = rowtotal(p071 p072 p073 p074 p075 p076 p077), missing 
	egen savingsFCat2008 = rowtotal(p071 p073), missing 	
	egen savingsCat2009 = rowtotal(d1_a d1_b d1_c d1_d d1_e d1_f d1_g ///
	d1_h d1_i), missing 
	egen savingsFCat2009 = rowtotal(d1_a d1_b d1_c d1_e), missing 	
		
* IV.7.d.ii Indexes (individual categories) for FE admin amounts ---------------
	gen savingsCatAd2008 = savingsCat2008
	gen savingsFCatAd2008 = savingsFCat2008
	egen savingsCatAd2009 = rowtotal(d1_c d1_d d1_e d1_f d1_g ///
		d1_h d1_i Gr0Aug09 d1_b), missing 
	egen savingsFCatAd2009 = rowtotal(d1_c d1_e Gr0Aug09 d1_b), ///
		missing 
		
* IV.7.e Savings by type -------------------------------------------------------
	rename p081m d1_cm2008
	rename p082m d1_dm2008
	rename p083m d1_em2008
	rename p084m d1_fm2008
	rename p085m d1_gm2008
	rename p086m d1_hm2008
	rename p087m d1_im2008
	
	foreach x in d1_am d1_bm d1_cm d1_dm d1_em d1_fm d1_gm d1_hm d1_im {
		rename `x' `x'2009
	}

* IV.7.e Other accounts (housing subsidy account + other bank account) ---------	
	gen otherAccounts2008 = d1_cm2008
	gen otherAccounts2009 = d1_cm2009 + d1_bm2009
	
// Note: Savings probability variables are created in section V.1. using  
// amount variables since index variables are not available for each category
	
*-------------------------------------------------------------------------------
* IV.8. Short-term borrowing variables
*-------------------------------------------------------------------------------
* IV.8.a Creates amount variables ----------------------------------------------
	//2008
	egen shortTermBorrowing2008 = rowtotal(p1703m p1704m p1705m p1707m p1708m ///
		p1709m p1710m p1711m p1712m p1713m p1714m p1715m p1716m p1717m ///
		p1719m), missing
	
	egen stBorrowFamily2008 = rowtotal(p1707m p1708m p1709m p1710m p1711m ///
		p1712m), missing
	
	egen stBorrowProviders2008 = rowtotal(p1715m p1716m p1717m), missing
	
	egen stBorrowInstitutions2008 = rowtotal(p1703m p1704m p1705m p1713m ///
		p1714m p1719m), missing
	
	//2009	
	egen shortTermBorrowing2009 = rowtotal(k1_dm k1_em k1_fm k1_hm k1_im /// 
		k1_jm k1_km k1_lm k1_mm k1_nm k1_om k1_pm k1_qm k1_rm ///
		k1_tm), missing
	
	egen stBorrowFamily2009 = rowtotal(k1_hm k1_im k1_jm k1_km k1_lm ///
		k1_mm), missing
	
	egen stBorrowProviders2009 = rowtotal(k1_pm k1_qm k1_rm), missing
	
	egen stBorrowInstitutions2009 = rowtotal(k1_dm k1_em k1_fm  k1_nm ///
		k1_om k1_tm), missing

	//long term borrowing
	egen longTermBorrowing2008 = rowtotal(p1701m p1702m), missing
	egen longTermBorrowing2009 = rowtotal(k1_am k1_bm k1_cm), missing
	
	gen mortgage2008 = p1701m
	gen mortgage2009 = k1_am
	
	gen formalLoan2008 = p1702m
	// 2009 survey breaks p1702 into two categories. 
	egen formalLoan2009 = rowtotal(k1_bm k1_cm), missing
	
* IV.8.b Creates index variables -----------------------------------------------
	
	// 2008
	egen stBorrowTotalIndex2008 = rowtotal(p1603 p1604 p1605 p1607 p1608 ///
		p1609 p1610 p1611 p1612 p1613 p1614 p1615 p1616 p1617 ///
		p1619), missing


	egen stBorrowFamilyIndex2008 = rowtotal(p1607 p1608 p1609 p1610 ///
		p1611 p1612), missing

	egen stBorrowProvidersIndex2008 = rowtotal(p1615 p1616 p1617), missing

	egen stBorrowInstitutionsIndex2008 = rowtotal(p1603 p1604 p1605 ///
		p1613 p1614 p1619), missing
	
	//2009
	egen stBorrowTotalIndex2009 = rowtotal(k1_d k1_e k1_f k1_h k1_i ///
		k1_j k1_k k1_l k1_m k1_n k1_o k1_p k1_q k1_r k1_t), missing
	
	egen stBorrowFamilyIndex2009 = rowtotal(k1_h k1_i k1_j k1_k k1_l ///
		k1_m), missing

	egen stBorrowProvidersIndex2009 = rowtotal(k1_p k1_q k1_r), missing

	egen stBorrowInstitutionsIndex2009 = rowtotal(k1_d k1_e k1_f k1_n ///
		k1_o k1_t), missing
		
	egen longTermBorrowingIndex2008 = rowtotal(p1601 p1602), missing
	//we group k1_bm and k1_cm because in the baseline survey they were 
	// grouped under p1702
	egen temp = rowtotal(k1_b k1_c), missing
	replace temp = 1 if temp > 1 & temp != .
	egen longTermBorrowingIndex2009 = rowtotal(k1_a temp), missing		

* IV.8.c Individual categories -------------------------------------------------
	*This step is simply renaming all the variables we used above. 
	
	*Short-term borrowing indexes
	local i = 1
	foreach x in p1603 p1604 p1605 p1607 p1608 p1609 p1610 p1611 p1612 ///
		p1613 p1614 p1615 p1616 p1617 p1619 {
		gen iBorrowCat`i'_2008 = `x'
		local i = `i' + 1
	}
	
	*Renaming short-term borrowing categories
	rename iBorrowCat1_2008 iBorrowFinancieras2008
	rename iBorrowCat2_2008 iBorrowCooperatives2008
	rename iBorrowCat3_2008 iBorrowMoneyLenders2008
	rename iBorrowCat4_2008 iBorrowParents2008
	rename iBorrowCat5_2008 iBorrowSonsDaughters2008
	rename iBorrowCat6_2008 iBorrowSiblings2008
	rename iBorrowCat7_2008 iBorrowPartner2008
	rename iBorrowCat8_2008 iBorrowFriends2008
	rename iBorrowCat9_2008 iBorrowRelatives2008
	rename iBorrowCat10_2008 iBorrowSuppliers2008
	rename iBorrowCat11_2008 iBorrowBusContacts2008
	rename iBorrowCat12_2008 iBorrowHealth2008
	rename iBorrowCat13_2008 iBorrowSchools2008
	rename iBorrowCat14_2008 iBorrowServProv2008
	rename iBorrowCat15_2008 iBorrowRegularClient2008
	
	local i = 1
	foreach x in k1_d k1_e k1_f k1_h k1_i k1_j k1_k k1_l k1_m ///
		k1_n k1_o k1_p k1_q k1_r k1_t {
		gen iBorrowCat`i'_2009 = `x'
		local i = `i' + 1
	}
	*Renaming short-term borrowing categories
	rename iBorrowCat1_2009 iBorrowFinancieras2009
	rename iBorrowCat2_2009 iBorrowCooperatives2009
	rename iBorrowCat3_2009 iBorrowMoneyLenders2009
	rename iBorrowCat4_2009 iBorrowParents2009
	rename iBorrowCat5_2009 iBorrowSonsDaughters2009
	rename iBorrowCat6_2009 iBorrowSiblings2009
	rename iBorrowCat7_2009 iBorrowPartner2009
	rename iBorrowCat8_2009 iBorrowFriends2009
	rename iBorrowCat9_2009 iBorrowRelatives2009
	rename iBorrowCat10_2009 iBorrowSuppliers2009
	rename iBorrowCat11_2009 iBorrowBusContacts2009
	rename iBorrowCat12_2009 iBorrowHealth2009
	rename iBorrowCat13_2009 iBorrowSchools2009
	rename iBorrowCat14_2009 iBorrowServProv2009
	rename iBorrowCat15_2009 iBorrowRegularClient2009
	
	
	*Short-term borrowing amounts:
	local i = 1
	foreach x in p1703m p1704m p1705m p1707m p1708m p1709m p1710m p1711m p1712m ///
		p1713m p1714m p1715m p1716m p1717m p1719m {
		gen borrowCat`i'_2008 = `x'
		local i = `i' + 1
	}
	
	*Renaming short-term borrowing categories
	rename borrowCat1_2008 borrowFinancieras2008
	rename borrowCat2_2008 borrowCooperatives2008
	rename borrowCat3_2008 borrowMoneyLenders2008
	rename borrowCat4_2008 borrowParents2008
	rename borrowCat5_2008 borrowSonsDaughters2008
	rename borrowCat6_2008 borrowSiblings2008
	rename borrowCat7_2008 borrowPartner2008
	rename borrowCat8_2008 borrowFriends2008
	rename borrowCat9_2008 borrowRelatives2008
	rename borrowCat10_2008 borrowSuppliers2008
	rename borrowCat11_2008 borrowBusContacts2008
	rename borrowCat12_2008 borrowHealth2008
	rename borrowCat13_2008 borrowSchools2008
	rename borrowCat14_2008 borrowServProv2008
	rename borrowCat15_2008 borrowRegularClient2008
	
	local i = 1
	foreach x in k1_dm k1_em k1_fm k1_hm k1_im k1_jm k1_km k1_lm k1_mm ///
		k1_nm k1_om k1_pm k1_qm k1_rm k1_tm {
		gen borrowCat`i'_2009 = `x'
		local i = `i' + 1
	}
	*Renaming short-term borrowing categories
	rename borrowCat1_2009 borrowFinancieras2009
	rename borrowCat2_2009 borrowCooperatives2009
	rename borrowCat3_2009 borrowMoneyLenders2009
	rename borrowCat4_2009 borrowParents2009
	rename borrowCat5_2009 borrowSonsDaughters2009
	rename borrowCat6_2009 borrowSiblings2009
	rename borrowCat7_2009 borrowPartner2009
	rename borrowCat8_2009 borrowFriends2009
	rename borrowCat9_2009 borrowRelatives2009
	rename borrowCat10_2009 borrowSuppliers2009
	rename borrowCat11_2009 borrowBusContacts2009
	rename borrowCat12_2009 borrowHealth2009
	rename borrowCat13_2009 borrowSchools2009
	rename borrowCat14_2009 borrowServProv2009
	rename borrowCat15_2009 borrowRegularClient2009
	


* IV.8.d Prob. of having any short-term borrowing category ---------------------

	//2008
	gen prShortTermBorrowingTot2008 = (stBorrowTotalIndex2008 > 0) if ///
		stBorrowTotalIndex2008 != .
	
	gen prShortTermBorrowFamily2008 = (stBorrowFamilyIndex2008 > 0) if ///
		stBorrowFamilyIndex2008 !=.
		
 	gen prShortTermBorrowProv2008 = (stBorrowProvidersIndex2008 >0) if ///
		stBorrowProvidersIndex2008 != .
		
	gen prShortTermBorrowInst2008 = (stBorrowInstitutionsIndex2008 >0) if ///
		stBorrowInstitutionsIndex2008 != .
		
    gen prborrowParents2008 =       (borrowParents2008 > 0)              if borrowParents2008!=. 
    gen prborrowSonsDaughters2008 = (borrowSonsDaughters2008 > 0)        if borrowSonsDaughters2008 !=.
    gen prborrowSiblings2008 =      (borrowSiblings2008 > 0)             if borrowSiblings2008 !=.
    gen prborrowPartner2008 =       (borrowPartner2008 >0)               if borrowPartner2008 !=.
    gen prborrowFriends2008 =       (borrowFriends2008 > 0 )             if borrowFriends2008 !=.
    gen prborrowRelatives2008 =     (borrowRelatives2008>0)              if borrowRelatives2008 !=.

    gen prlongTermBorrowing2008 = (longTermBorrowingIndex2008 >0) if ///
		longTermBorrowingIndex2008 != .
    
	//2009 
	gen prShortTermBorrowingTot2009 = (stBorrowTotalIndex2009 > 0) if ///
		stBorrowTotalIndex2009 != .

	gen prShortTermBorrowFamily2009 = (stBorrowFamilyIndex2009 > 0) if ///
		stBorrowFamilyIndex2009 !=.
		
 	gen prShortTermBorrowProv2009 = (stBorrowProvidersIndex2009 >0) if ///
		stBorrowProvidersIndex2009 != .
		
	gen prShortTermBorrowInst2009 = (stBorrowInstitutionsIndex2009 >0) if ///
		stBorrowInstitutionsIndex2009 != .
	
    gen prborrowParents2009 =       (borrowParents2009 > 0)              if borrowParents2009!=. 
    gen prborrowSonsDaughters2009 = (borrowSonsDaughters2009 > 0)        if borrowSonsDaughters2009 !=.
    gen prborrowSiblings2009 =      (borrowSiblings2009 > 0)             if borrowSiblings2009 !=.
    gen prborrowPartner2009 =       (borrowPartner2009 >0)               if borrowPartner2009 !=.
    gen prborrowFriends2009 =       (borrowFriends2009 > 0 )             if borrowFriends2009 !=.
    gen prborrowRelatives2009 =     (borrowRelatives2009>0)              if borrowRelatives2009 !=.
    
    gen prlongTermBorrowing2009 = (longTermBorrowingIndex2009 >0) if ///
		longTermBorrowingIndex2009 != .
*-------------------------------------------------------------------------------
* IV.9. Lending amount
*-------------------------------------------------------------------------------
* IV.9.a Creates amount variables ----------------------------------------------
	
	//2008
	egen totalLending2008 = rowtotal(p1901m p1902m p1903m p1904m p1905m /// 
		p1906m p1907m p1908m p1909m), missing 

	egen familyLending2008 = rowtotal(p1901m p1902m p1903m p1904m p1905m ///
		p1909m), missing 

	egen businessLending2008 = rowtotal(p1906m p1907m p1908m), missing 

	//2009
	egen totalLending2009 = rowtotal(k2_1m k2_2m k2_3m k2_4m k2_5m ///
		k2_6m k2_7m k2_8m k2_9m), missing 

	egen familyLending2009 = rowtotal(k2_1m k2_2m k2_3m k2_4m k2_5m ///
		k2_8m), missing 

	egen businessLending2009 = rowtotal(k2_6m k2_7m k2_9m), missing 	
 
 * IV.9.b Creates index variables ----------------------------------------------
 	 
	//2008
	egen totalLendingIndex2008 = rowtotal(p1801 p1802 p1803 p1804 p1805 /// 
		p1806 p1807 p1808 p1809), missing 	
	
	egen familyLendingIndex2008 = rowtotal(p1801 p1802 p1803 p1804 p1805 /// 
		p1809), missing 	
	
	egen businessLendingIndex2008 = rowtotal(p1806 p1807 p1808), missing 

	//2009
	egen totalLendingIndex2009 = rowtotal(k2_1 k2_2 k2_3 k2_4 k2_5 k2_6 ///
		k2_7 k2_8 k2_9), missing 	
	
	egen familyLendingIndex2009 = rowtotal(k2_1 k2_2 k2_3 k2_4 k2_5 k2_8 ///
		), missing 	
	
	egen businessLendingIndex2009 = rowtotal(k2_6 k2_7 k2_9), missing 



* IV.9.c Prob. of having any lending ---------------------

	//2008
	gen prLendingTot2008 = (totalLendingIndex2008 > 0) if ///
		totalLendingIndex2008 != .
	
	gen prLendingfamily2008 = (familyLendingIndex2008 > 0) if ///
		familyLendingIndex2008 !=.
		
 	gen prLendingbusiness2008 = (businessLendingIndex2008 >0) if ///
		businessLendingIndex2008 != .	
	
	//2009 
	gen prLendingTot2009 = (totalLendingIndex2009 > 0) if ///
		totalLendingIndex2009 != .
	
	gen prLendingfamily2009 = (familyLendingIndex2009 > 0) if ///
		familyLendingIndex2009 !=.
		
 	gen prLendingbusiness2009 = (businessLendingIndex2009 >0) if ///
		businessLendingIndex2009 != .	

*-------------------------------------------------------------------------------
* IV.10. Net savings 
*-------------------------------------------------------------------------------
	
	gen t56 = -1*shortTermBorrowing2008 if shortTermBorrowing2008 != .
	gen t57 = -1*shortTermBorrowing2009 if shortTermBorrowing2009 != .
	gen t58 = -1*longTermBorrowing2008 if longTermBorrowing2008 != .
	gen t59 = -1*longTermBorrowing2009 if longTermBorrowing2009 != .
	egen netSavings2008 = rowtotal(allSavings2008 t56 t58), missing
	egen netSavings2009 = rowtotal(allSavings2009 t57 t59), missing
	
	egen netSavingsAd2008 = rowtotal(allSavingsAd2008 t56 t58), missing
	egen netSavingsAd2009 = rowtotal(allSavingsAd2009 t57 t59), missing
	
	egen changeTotalWealth2008 = rowtotal(allSavings2008 t56 t58 totalLending2008), missing
	egen changeTotalWealth2009 = rowtotal(allSavings2009 t57 t59 totalLending2009), missing

	egen changeTotalWealthAd2008 = rowtotal(allSavingsAd2008 t56 t58 totalLending2008), missing
	egen changeTotalWealthAd2009 = rowtotal(allSavingsAd2009 t57 t59 totalLending2009), missing
*-------------------------------------------------------------------------------
* IV.11. Group size
*-------------------------------------------------------------------------------
* This variable is inferred from the randomization data

	gen groupId = id_banco

*-------------------------------------------------------------------------------
* IV.12. Sample indicator
*-------------------------------------------------------------------------------
* This variable marks subjects that answered both surveys (1 if they did, 0 
* otherwise) and is used in defining samples for most regressions below.
	
	gen inSample = (source != "")

*-------------------------------------------------------------------------------
* IV.13. Regret
*-------------------------------------------------------------------------------

	gen regretsNotSaving = (p12 == 1 | p12 == 2) if p12 != 9

*-------------------------------------------------------------------------------
* IV.14. Consumption cutbacks
*-------------------------------------------------------------------------------
	
	gen mealsCutback2008 = p2301
	gen mealsCutback2009 = c3_01
	gen meatCutback2008 = p2302
	gen meatCutback2009 = c3_02
	gen medicinesCutback2008 = p2303
	gen medicinesCutback2009 = c3_03
	gen schoolSuppliesCutback2008 = p2305
	gen schoolSuppliesCutback2009 = c3_05
	gen clothingCutback2008 = p2306
	gen clothingCutback2009 = c3_06
	gen schoolSnacksCutback2008 = p2307
	gen schoolSnacksCutback2009 = c3_07
	gen transportCutback2008 = p2308
	gen transportCutback2009 = c3_08
	gen eatingOutCutback2008 = p2309
	gen eatingOutCutback2009 = c3_09
	
	egen numberConsumptionCutbacks2008 = rowtotal(p2301 p2302 p2303 p2305 ///
		p2306 p2307 p2308 p2309), missing
	gen consumptionCutbacks2008 = (numberConsumptionCutbacks2008 > 0) if ///
		numberConsumptionCutbacks2008 != .
		
	egen numberConsumptionCutbacks2009 = rowtotal(c3_01 c3_02 c3_03 c3_05 ///
		c3_06 c3_07 c3_08 c3_09), missing
	gen consumptionCutbacks2009 = (numberConsumptionCutbacks2009 > 0) if ///
		numberConsumptionCutbacks2009 != .
	
*-------------------------------------------------------------------------------
* IV.15. Economic shock dummies
*-------------------------------------------------------------------------------

	

		gen economicShock2008 = (p2184 == 4 | p2185 == 5 | p2186 == 6 | ///		218 corresponds to 21st question 8th subpart of baseline survey - economic downturn - and 4,5,6 means last 3 months - Jan, Feb, Mar.
					 p2124 == 4 | p2125 == 5 | p2126 == 6)	//		212 corresponds to 21st question 2nd subpart of baseline survey - job loss - 4,5,6 last 3 months - jan, feb, march.
				
		tab p2184, m nolabel	
		tab p2185, m nolabel
		tab p2186, m nolabel	
		tab p2124, m nolabel	
		tab p2125, m nolabel
		tab p2126, m nolabel	
		count if p2184 == . & p2185 == . & p2186 == . & p2124 ==. &  p2125 ==. & p2126 ==. //2,565
				
		
		gen economicShock2009 = (c1_1_jun == 6 | c1_1_may == 5 | ///
		c1_1_abr == 4 | c1_7_jun == 6 | c1_7_may == 5 | c1_7_abr == 4)


		tab c1_1_jun, m nolabel	
		tab c1_1_may, m nolabel
		tab c1_1_abr, m nolabel	
		tab c1_7_jun, m nolabel	
		tab c1_7_may, m nolabel
		tab c1_7_abr, m nolabel	
		count if c1_1_jun == . & c1_1_may == . & c1_1_abr == . & c1_7_jun ==. &  c1_7_may ==. & c1_7_abr ==. //2,565
				
		


	gen jobLoss2008 = (p2124 == 4 | p2125 == 5 | p2126 == 6)
	gen jobLoss2009 = (c1_1_jun == 6 | c1_1_may == 5 | c1_1_abr == 4)
	
	gen bizDown2008 = (p2184 == 4 | p2185 == 5 | p2186 == 6)
	gen bizDown2009 = (c1_7_jun == 6 | c1_7_may == 5 | c1_7_abr == 4)
	
* Shock variables by month 
*-------------------------------------------------------------------------------

*For analysis of consupmtion smoothening hypothesis, create a shock variable by month
*Ideally we'd need shock in Jul-Dec 08 and Jan-Jul 09 But from baseline surveys we only have
*apr, may, jun 08 (not useful), hence stick to endline survey and create monthly shock 
*variables jan-jun09 (dont have it for july09 either)
    gen ecShm1y2009 = (c1_1_ene == 1 | c1_7_ene == 1) 
    gen ecShm2y2009 = (c1_1_feb == 2 | c1_7_feb == 2)
    gen ecShm3y2009 = (c1_1_mar == 3 | c1_7_mar == 3)
    gen ecShm4y2009 = (c1_1_abr == 4 | c1_7_abr == 4)
    gen ecShm5y2009 = (c1_1_may == 5 | c1_7_may == 5)
    gen ecShm6y2009 = (c1_1_jun == 6 | c1_7_jun == 6)

*To conduct shock analysis for each shock, create individual shock dummies by month.
*1. Job loss 
  gen jobShm1y2009 = (c1_1_ene == 1 )
  gen jobShm2y2009 = (c1_1_feb == 2 )
  gen jobShm3y2009 = (c1_1_mar == 3 )
  gen jobShm4y2009 = (c1_1_abr == 4 )
  gen jobShm5y2009 = (c1_1_may == 5 )
  gen jobShm6y2009 = (c1_1_jun == 6 )
*2. Medical expense eco shock
  gen medShm1y2009 = (c1_2_ene == 1 )
  gen medShm2y2009 = (c1_2_feb == 2 )
  gen medShm3y2009 = (c1_2_mar == 3 )
  gen medShm4y2009 = (c1_2_abr == 4 )
  gen medShm5y2009 = (c1_2_may == 5 )
  gen medShm6y2009 = (c1_2_jun == 6 )
*3. Birth of child in last 6 months
  gen birShm1y2009 = (c1_3_ene == 1 )
  gen birShm2y2009 = (c1_3_feb == 2 )
  gen birShm3y2009 = (c1_3_mar == 3 )
  gen birShm4y2009 = (c1_3_abr == 4 )
  gen birShm5y2009 = (c1_3_may == 5 )
  gen birShm6y2009 = (c1_3_jun == 6 )
*4. Increase in the no. of ppl to keep 
  gen pplShm1y2009 = (c1_4_ene == 1 )
  gen pplShm2y2009 = (c1_4_feb == 2 )
  gen pplShm3y2009 = (c1_4_mar == 3 )
  gen pplShm4y2009 = (c1_4_abr == 4 )
  gen pplShm5y2009 = (c1_4_may == 5 )
  gen pplShm6y2009 = (c1_4_jun == 6 )
*5. Separation from partner in last 6 months
  gen sepShm1y2009 = (c1_5_ene == 1 )
  gen sepShm2y2009 = (c1_5_feb == 2 )
  gen sepShm3y2009 = (c1_5_mar == 3 )
  gen sepShm4y2009 = (c1_5_abr == 4 )
  gen sepShm5y2009 = (c1_5_may == 5 )
  gen sepShm6y2009 = (c1_5_jun == 6 )
*6. Robbery in last 6 months. 
  gen robShm1y2009 = (c1_6_ene == 1 )
  gen robShm2y2009 = (c1_6_feb == 2 )
  gen robShm3y2009 = (c1_6_mar == 3 )
  gen robShm4y2009 = (c1_6_abr == 4 )
  gen robShm5y2009 = (c1_6_may == 5 )
  gen robShm6y2009 = (c1_6_jun == 6 )
*7. Business down
  gen bizShm1y2009 = (c1_7_ene == 1 )
  gen bizShm2y2009 = (c1_7_feb == 2 )
  gen bizShm3y2009 = (c1_7_mar == 3 )
  gen bizShm4y2009 = (c1_7_abr == 4 )
  gen bizShm5y2009 = (c1_7_may == 5 )
  gen bizShm6y2009 = (c1_7_jun == 6 )
*8. Marriage of a child or yourself in last 6 months
  gen marShm1y2009 = (c1_8_ene == 1 )
  gen marShm2y2009 = (c1_8_feb == 2 )
  gen marShm3y2009 = (c1_8_mar == 3 )
  gen marShm4y2009 = (c1_8_abr == 4 )
  gen marShm5y2009 = (c1_8_may == 5 )
  gen marShm6y2009 = (c1_8_jun == 6 )
*9. Death of someone you had to take care of
  gen detShm1y2009 = (c1_9_ene == 1 )
  gen detShm2y2009 = (c1_9_feb == 2 )
  gen detShm3y2009 = (c1_9_mar == 3 )
  gen detShm4y2009 = (c1_9_abr == 4 )
  gen detShm5y2009 = (c1_9_may == 5 )
  gen detShm6y2009 = (c1_9_jun == 6 )
  
  
  // Save this as monthly data and use this for deposit and withdrawal analysis. 
  save $inputPath/monthly_data, replace
  
*------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* IV.16. Individual Treatment Effects						
*-------------------------------------------------------------------------------
* IV.16.a Creates level variables ----------------------------------------------
* This variable is expresed in terms of standard deviations. Transformation is 
* done in section V.4.c 
	
	// we invert the order so the higher the bumber, higher the anxiety 
	// reported by subject
	gen anxietyFuture2008 		= 1 if p331 == 4 & p331 != 9 
	replace anxietyFuture2008 	= 2 if p331 == 3 & p331 != 9 
	replace anxietyFuture2008 	= 3 if p331 == 2 & p331 != 9 
	replace anxietyFuture2008 	= 4 if p331 == 1 & p331 != 9 

	gen anxietyFuture2009 		= 1 if m3_1 == 4 & m3_1 != 9 & m3_1 != .
	replace anxietyFuture2009 	= 2 if m3_1 == 3 & m3_1 != 9  & m3_1 != .
	replace anxietyFuture2009	= 3 if m3_1 == 2 & m3_1 != 9 & m3_1 != . 
	replace anxietyFuture2009 	= 4 if m3_1 == 1 & m3_1 != 9 & m3_1 != . 

	gen economicDifficulty2008 = p25 if p25 != 99
	gen economicDifficulty2009 = c5 if c5 != 99 & c5 != . 

// Variable for tansfer analysis
*-------------------------------------------------------------------------------
    gen transfers_3months2008 = p26 if p26 !=99
    gen transfers_3months2009 = c6 if c6!=00 & c6 !=.
*-------------------------------------------------------------------------------

* IV.16.b Creates dummy variables ----------------------------------------------

	//2008
	gen anxietyFutureFirst2008 = (p331 <= 3) if p331 != 9
	gen anxietyFutureSecond2008 = (p331 <= 2) if p331 != 9
	gen anxietyFutureThird2008 = (p331 <= 1) if p331 != 9
	
	//2009
	gen anxietyFutureFirst2009 = (m3_1 <= 3) if m3_1 != 9 & m3_1 != .
	gen anxietyFutureSecond2009 = (m3_1 <= 2) if m3_1 != 9 & m3_1 != .
	gen anxietyFutureThird2009 = (m3_1 <= 1) if m3_1 != 9 & m3_1 != .
	
	//2008
	gen economicDifficultyFirst2008 = (p25 > 2) if p25 != 99
	gen economicDifficultySecond2008 = (p25 > 4) if p25 != 99
	gen economicDifficultyThird2008 = (p25 > 6) if p25 != 99
	gen economicDifficultyFourth2008 = (p25 > 8) if p25 != 99
	
	//2009
	gen economicDifficultyFirst2009 = (c5 > 2) if c5 != 99 & c5 != .
	gen economicDifficultySecond2009 = (c5 > 4) if c5 != 99 & c5 != .
	gen economicDifficultyThird2009 = (c5 > 6) if c5 != 99 & c5 != .
	gen economicDifficultyFourth2009 = (c5 > 8) if c5 != 99 & c5 != .
	
*-------------------------------------------------------------------------------
* IV.17. Female									
*-------------------------------------------------------------------------------

	gen female = (a_01 == 2) if a_01 != . & a_01 != 9

*-------------------------------------------------------------------------------
* IV.18. Number of children at home
*-------------------------------------------------------------------------------

	egen childrenAtHome = rowtotal(a_03_1 a_03_2), missing

*-------------------------------------------------------------------------------
* IV.19. Demand effects variable
*-------------------------------------------------------------------------------	
	
	gen difficultySurvey = p_1 if p_1 != 9 & p_1 != .
	gen satisfactionFE = g4 if g4 != 9 & g4 != .

*-------------------------------------------------------------------------------
* IV.20. Head of household dummy						
*-------------------------------------------------------------------------------	

	gen headHousehold = (jefehogar == "Y") if jefehogar != ""

*-------------------------------------------------------------------------------
* IV.21. Conflict with partner over money dummy
*-------------------------------------------------------------------------------

	gen conflictsMoney = (p32 == 1) if p32 != 9 & p32 != .

*-------------------------------------------------------------------------------
* IV.22. Socially taxed dummy
*-------------------------------------------------------------------------------

	gen sociallyTaxed = (familyLendingIndex2008 > 0 & ///
		stBorrowFamilyIndex2008 == 0) if familyLendingIndex2008 != . ///
		& stBorrowFamilyIndex2008 != .

*-------------------------------------------------------------------------------
* IV.23. Hyperbolic preferences							
*-------------------------------------------------------------------------------
	
	gen hyperbolicPreferences = ( ///
		(p01 == 1 & p02 == 2 & p27 == 2 & (p28 == 2 | p28 == 9)) | ///
		(p01 == 1 & p02 == 1 & p03 == 2 & p27 == 2 & (p28 == 2 | ///
			p28 == 9)) | ///
		(p01 == 1 & p02 == 1 & p03 == 2 & p27 == 1 & p28 == 2) | ///
		(p01 == 1 & p02 == 1 & (p03 == 1 | p03 == 9) & p27 == 2 & ///
			(p28 == 2 | p28 == 9)) | ///
		(p01 == 1 & p02 == 1 & (p03 == 1 | p03 == 9) & p27 == 1 ///
			& p28 == 2) | ///
		(p01 == 1 & p02 == 1 & (p03 == 1 | p03 == 9) & p27 == 1 & ///
			p28 == 1 & p29 == 2))

*-------------------------------------------------------------------------------
* IV.24. Fear of savings stolen
*-------------------------------------------------------------------------------
	
	gen fearStolen = (p11 == 1) if p11 != 9 & p11 != .

*-------------------------------------------------------------------------------
* IV.25. Lent or owe to family dummies
*-------------------------------------------------------------------------------

	gen lentFamily = (familyLendingIndex2008 > 0) if ///
		familyLendingIndex2008 != . 
	gen owesFamily = (stBorrowFamilyIndex2008 > 0) if ///
		stBorrowFamilyIndex2008 != . 

*-------------------------------------------------------------------------------
* IV.26. Intimidated by bank		
*-------------------------------------------------------------------------------	
	
	gen intimidatedBank2008 = p334 if p334 != 9 & p334 != .
	gen intimidatedBank2009 = m3_4 if m3_4 != 9 & m3_4 != .
	
	gen binIntimBank2008 = (intimidatedBank2008 == 1 | intimidatedBank2008 == 2) if p334 != 9 & p334 != .
	gen binIntimBank2009 = (intimidatedBank2009 == 1 | intimidatedBank2009 == 2) if m3_4 != 9 & m3_4 != .

*-------------------------------------------------------------------------------
* IV.27. Family dynamics			
*-------------------------------------------------------------------------------

	gen expDecision2008 = (p06 == 1) if p06 != 9 /*& p06 != 3*/
	gen savingDecision2008 = (p14 == 1) if p14 != 9 /*& p14 != 3*/
	gen hideSavingsFamily2008 = (p10 == 1) if p10 != 9 
	gen hideSavingsSO2008 = (p09 == 2) if p09 != 9 /*& p09 != 3*/
	gen borrowSO2008 = (p31 == 1) if p31 != 9 /*& p31 != 3*/ 
	gen troubleSO2008 = (p32 == 1) if p32 != 9 /*& p32 != 3*/  

	gen expDecision2009 = (b_3 == 1) if b_3 != 9 /*& b_3 != 3*/  & b_3 != .
	gen savingDecision2009 = (savingDecision == 1) if savingDecision ///
		!= 9 /*& savingDecision != 3*/ & savingDecision != .
	gen hideSavingsFamily2009 = (hideSavingsFamily == 1) if ///
		hideSavingsFamily != 9  & hideSavingsFamily != . 
	gen hideSavingsSO2009 = (hideSavingsSO == 2) if hideSavingsSO != 9 & ///
		/*hideSavingsSO != 3 &*/ hideSavingsSO != .

	gen borrowSO2009 = (m1 == 1) if m1 != 9 /*& m1 != 3*/ & m1 != .
	gen troubleSO2009 = (m2 == 1) if m2 != 9 /*& m2 != 3*/ & m1 != .

*-------------------------------------------------------------------------------
* IV.28. Bulky expenditures/Gift Giving			
*-------------------------------------------------------------------------------

	gen boughtElectronics2008 = (p053 == 1) if p053 != 9
	gen madeInvestment2008 = (p055 == 1) if p055 != 9 
	gen improveHome2008 = (p051 == 1) if p051 != 9
	gen prePayFE2008 = (p056 == 1) if p056 != 9 
	gen giftGiving2008 = (p26 == 1) if p26!=9
	
	gen housingSubsidy2008 = (p04 == 1) if p04 != 9
	
	gen boughtElectronics2009 = (b_2_2 == 1) if b_2_2 != . & b_2_2 != 9
	gen madeInvestment2009 = (b_2_3 == 1) if b_2_3 != . & b_2_3 != 9
	gen improveHome2009 = (b_2_1 == 1) if b_2_1 != . & b_2_1 != 3
	gen prePayFE2009 = (b_2_4 == 1) if b_2_4 != . & b_2_4 != 9
	gen giftGiving2009 = (c6 == 1) if c6 != . & c6 != 9
	gen housingSubsidy2009 = (b_1 == 1) if b_1 != . & b_1 != 9
	
	gen anyBulkyExp2008 = 0 if (boughtElectronics2008 == 0 & ///
		madeInvestment2008 == 0 & improveHome2008 == 0 & ///
		giftGiving2008 == 0 & housingSubsidy2008 == 0)	
	replace anyBulkyExp2008 = 1 if (boughtElectronics2008 == 1 | ///
		madeInvestment2008 == 1 | improveHome2008 == 1 | /// 
		giftGiving2008 == 1 |  housingSubsidy2008 == 1)	
	gen anyBulkyExp2009 = 0 if (boughtElectronics2009 == 0 & ///
		madeInvestment2009 == 0 & improveHome2009 == 0 & /// 
		giftGiving2009 == 0 & housingSubsidy2009 == 0)	
	replace anyBulkyExp2009 = 1 if (boughtElectronics2009 == 1 | ///
		madeInvestment2009 == 1 | improveHome2009 == 1 | ///
		giftGiving2009 == 1 | housingSubsidy2009 == 1 | ///
		housingSubsidy2009 == 1)	
	
	egen numberBulkyExp2008 = rowtotal(boughtElectronics2008 ///
		madeInvestment2008 improveHome2008 giftGiving2008 ///
		housingSubsidy2008), missing
	egen numberBulkyExp2009 = rowtotal(boughtElectronics2009 ///
		madeInvestment2009 improveHome2009 giftGiving2009 ///
		housingSubsidy2009), missing

	
*-------------------------------------------------------------------------------
* IV.29. Income and Household Size			
*-------------------------------------------------------------------------------

	/*Here we combine the administrative information from the beginning of 
	the study with the self-reported measures from the follow-up survey*/
	
	/*Imputing the mean of each income bracket, except for the top bracket
	(1,251,000 CLP or more), for which we keep the lower bound (1,251,000)*/
	gen selfReportedIncome2009 = 32000 if a_07 == 1
	replace selfReportedIncome2009 = 98500 if a_07 == 2
	replace selfReportedIncome2009 = 191500 if a_07 == 3
	replace selfReportedIncome2009 = 300500 if a_07 == 4
	replace selfReportedIncome2009 = 400500 if a_07 == 5
	replace selfReportedIncome2009 = 550500 if a_07 == 6
	replace selfReportedIncome2009 = 750500 if a_07 == 7
	replace selfReportedIncome2009 = 950500 if a_07 == 8
	replace selfReportedIncome2009 = 1150500 if a_07 == 9	
	replace selfReportedIncome2009 = 1251000 if a_07 == 10
	
	*Generating Household Size variable for 2009
	egen householdSize2009 = rowtotal(a_03_1 a_03_2 a_03_3 a_03_4), missing
	*Values of 0 in household size are mistakes
	replace householdSize2009 = . if householdSize2009==0
	
	*Generating Income per Capita variable
	gen incomePerCap2008 = incomePerCapita 
	gen incomePerCap2009 = selfReportedIncome2009/householdSize2009
	
	*Generating Household Income variable
	gen hhIncome2008 = hogarini if hogarini >= 25000 & ///
		hogarini <= 5000000
	gen hhIncome2009 = selfReportedIncome2009

	*Generating log household income variable
	gen lnHHIncome2008 = ln(hogarini) if hogarini >= 25000 & ///
		hogarini <= 5000000
	gen lnHHIncome2009 = ln(selfReportedIncome2009)
	
	*Generating log household income variable
	gen lnIncomePerCap2008 = ln(incomePerCap2008)
	gen lnIncomePerCap2009 = ln(incomePerCap2009)
	
	*Generating log household income variable
	gen lnIncomePerCapA2008 = lnHHIncome2008/householdSize2008
	gen lnIncomePerCapA2009 = lnHHIncome2009/householdSize2009 
			
*-------------------------------------------------------------------------------
* IV.30. Indicator Variables of Self-Reported Deposits and Withdrawals			
*-------------------------------------------------------------------------------

	gen depositFE2009 = (d2_a == 1) if d2_a!=9 & inSample==1 & d2_a !=.
	*Comparing FE response with actual admin. data
	gen depositFEAcct2009 = 0 if inSample==1
	replace depositFEAcct2009 = 1 if totalDepositedJan09Aug09>0 & ///
		totalDepositedJan09Aug09!=. & inSample==1
	gen depositHousingAcct2009 = (d2_b == 1) if d2_b!= 9 & inSample==1 & d2_b !=.
	gen depositOtherAcct2009 = (d2_c == 1) if d2_c!= 9 & inSample==1 & d2_c !=.
	
	gen withdrawFE2009 = (d3_a == 1) if d3_a!= 9  & inSample==1 & d3_a!= .
	*Comparing FE response with actual admin. data
	gen withdrawFEAcct2009 = 0 if inSample==1 
	replace withdrawFEAcct2009 = 1 if totalWithdrawalJan09Aug09>0 & ///
		totalWithdrawalJan09Aug09!=. & inSample==1 
	gen withdrawHousingAcct2009 = (d3_b == 1) if d3_b!= 9 & inSample==1 & d3_b !=.
	gen withdrawOtherAcct2009 = (d3_c == 1) if d3_c!= 9 &  inSample==1 & d3_c !=.
		
	gen anyDeposit2009 = 0 if (depositFE2009 == 0 & ///
		depositHousingAcct2009 == 0 & depositOtherAcct2009 == 0)	
	replace anyDeposit2009 = 1 if (depositFE2009 == 1 | ///
		depositHousingAcct2009 == 1 | depositOtherAcct2009 == 1)
		
	gen anyDepositAcct2009  = 0 if (depositFEAcct2009 == 0 & ///
		depositHousingAcct2009 == 0 & depositOtherAcct2009 == 0)
	replace anyDepositAcct2009 = 1 if (depositFEAcct2009 == 1 | ///
		depositHousingAcct2009 == 1 | depositOtherAcct2009 == 1)
	
	gen anyWithdrawal2009 = 0 if (withdrawFE2009 == 0 & ///
		withdrawHousingAcct2009 == 0 & withdrawOtherAcct2009 == 0)	
	replace anyWithdrawal2009 = 1 if (withdrawFE2009 == 1 | ///
		withdrawHousingAcct2009 == 1 | withdrawOtherAcct2009 == 1)
		
	gen anyWithdrawalAcct2009 = 0 if (withdrawFEAcct2009 == 0 & ///
		withdrawHousingAcct2009 == 0 & withdrawOtherAcct2009 == 0)	
	replace anyWithdrawalAcct2009 = 1 if (withdrawFEAcct2009 == 1 | ///
		withdrawHousingAcct2009 == 1 | withdrawOtherAcct2009 == 1)	
	
	egen numberDeposits2009 = rowtotal(depositFE2009 depositHousingAcct2009 ///
		depositOtherAcct2009), missing
	egen numberWithdrawals2009 = rowtotal(withdrawFE2009 withdrawHousingAcct2009 ///
		withdrawOtherAcct2009), missing
		
	egen numberDepositsAcct2009 = rowtotal(depositFEAcct2009 depositHousingAcct2009 ///
		depositOtherAcct2009), missing
	egen numberWithdrawalsAcct2009 = rowtotal(withdrawFEAcct2009 withdrawHousingAcct2009 ///
		withdrawOtherAcct2009), missing
		
*-------------------------------------------------------------------------------
* IV.31. Savings and Borrowing divided by Income			
*-------------------------------------------------------------------------------

	gen fSavingsIncPerCap2008 = formalSavings2008/incomePerCap2008
	gen fSavingsIncPerCap2009 = formalSavings2009/incomePerCap2009
	gen allSavingsIncPerCap2008 = allSavings2008/incomePerCap2008
	gen allSavingsIncPerCap2009 = allSavings2009/incomePerCap2009
	gen stBorrowingIncPerCap2008 = shortTermBorrowing2008/incomePerCap2008
	gen stBorrowingIncPerCap2009 = shortTermBorrowing2009/incomePerCap2009
	gen stBorFamIncPerCap2008 = stBorrowFamily2008/incomePerCap2008
	gen stBorFamIncPerCap2009 = stBorrowFamily2009/incomePerCap2009
	gen stBorProvIncPerCap2008 = stBorrowProviders2008/incomePerCap2008
	gen stBorProvIncPerCap2009 = stBorrowProviders2009/incomePerCap2009
	gen stBorInstIncPerCap2008 = stBorrowInstitutions2008/incomePerCap2008
	gen stBorInstIncPerCap2009 = stBorrowInstitutions2009/incomePerCap2009
			

*-------------------------------------------------------------------------------
* IV.32. Creates propensity score to be used in table A9 IPW			
*-------------------------------------------------------------------------------
	
	// Currently missing, but in balance table: 
	// incomePerCapita2008, savingsCatAd2008
	// shortTermBorrowing2008, totalLendingIndex2008, anxietyFuture2008, economicDifficulty2008
	// regretsNotSaving, sociallyTaxed, conflictsMoney, economicShock2008
	
	// imputes missings on the covariates to avoid loosing observations
	foreach x in accountAccess activeUser stBorrowProvidersIndex2008 ///
		mealsCutback2008 hyperbolicPreferences age headHousehold ///
		formalSavings2008 allSavings2008 numWithdrawalsJul08Aug09 ///
		householdSize2008 hadFormalSavings2008 totalLending2008 ///
		educationYears consumptionCutbacks2008 hasPrevAccount ///
		stBorrowInstitutionsIndex2008 /// NEW VARS BELOW
		incomePerCap2008 savingsCatAd2008 shortTermBorrowing2008 ///
		totalLendingIndex2008 anxietyFuture2008 economicDifficulty2008 /// 
		regretsNotSaving sociallyTaxed conflictsMoney economicShock2008 {
		
		gen ip_`x' = `x'
		quietly: sum `x' 
		replace ip_`x' = `r(mean)' if ip_`x' == .
		
	}
	
	//computes probability of being in the sample
	probit inSample ip_accountAccess ip_activeUser ip_stBorrowProvidersIndex2008 ///
		ip_mealsCutback2008 ip_hyperbolicPreferences ip_age ip_headHousehold ///
		ip_formalSavings2008 ip_allSavings2008 ip_numWithdrawalsJul08Aug09 ///
		ip_householdSize2008 ip_hadFormalSavings2008 ip_totalLending2008 ///
		ip_educationYears ip_consumptionCutbacks2008 ip_stBorrowInstitutionsIndex2008 ///
		ip_hasPrevAccount  c.ip_formalSavings2008#c.ip_householdSize2008 ///
		c.ip_headHousehold#c.ip_formalSavings2008 c.ip_activeUser#c.ip_age ///
		c.ip_stBorrowProvidersIndex2008#c.ip_headHousehold ///
		c.ip_accountAccess#c.ip_activeUser c.ip_hyperbolicPreferences#c.ip_age ///
		c.ip_hadFormalSavings2008#c.ip_stBorrowInstitutionsIndex2008 ///
		c.ip_stBorrowProvidersIndex2008#c.ip_stBorrowProvidersIndex2008 ///
		c.ip_accountAccess#c.ip_stBorrowProvidersIndex2008 ///
		c.ip_numWithdrawalsJul08Aug09#c.ip_hadFormalSavings2008 ///
		c.ip_numWithdrawalsJul08Aug09#c.ip_totalLending2008 ///
		c.ip_formalSavings2008#c.ip_hadFormalSavings2008 ///
		c.ip_stBorrowProvidersIndex2008#c.ip_mealsCutback2008 /// NEW VARS BELOW
		ip_stBorrowInstitutionsIndex2008 /// NEW VARS BELOW
		ip_incomePerCap2008 ip_savingsCatAd2008 ip_shortTermBorrowing2008 ///
		ip_totalLendingIndex2008 ip_anxietyFuture2008 ip_economicDifficulty2008 /// 
		ip_regretsNotSaving ip_sociallyTaxed ip_conflictsMoney ip_economicShock2008, ///
		vce(cluster groupId)
	
	//yields propensity score
	predict ps, pr 								
	
	//calculates inverse probability weight
	gen ipw_v2 = 1 / ps if inSample == 1
	replace ipw_v2 = 1 / (1 - ps) if inSample == 0
	
*-------------------------------------------------------------------------------
* IV.33. Financial Literacy and Education Variables
*-------------------------------------------------------------------------------
* IV.33.a Financial Literacy score ---------------------------------------------
	gen finlit_1=.
	*175,000 is the correct answer
	replace finlit_1=1 if p341cod==175000
	replace finlit_1=0 if p341cod!=175000
	replace finlit_1=0 if  p341cod==98000000
	replace finlit_1=. if  p341cod==99999999
	*Question 2
	gen finlit_2=.
	*150 is the correct answer
	replace finlit_2=1 if p342cod==150
	replace finlit_2=0 if p342cod!=150
	replace finlit_2=0 if  p342cod==98000000
	replace finlit_2=. if  p342cod==99999999
	*Question 3
	gen finlit_3=.
	replace finlit_3=1 if p343cod==242000
	*242,000 is the correct answer
	replace finlit_3=0 if p343cod!=242000
	replace finlit_3=0 if  p343cod==98000000
	replace finlit_3=. if  p343cod==99999999
	*Create a total finlit score
	gen finlit_score=finlit_1 + finlit_2 + finlit_3
	*Find median finlit score
	qui sum finlit_score, detail
	di r(p50)
	*2
	
* IV.33.b Dummy for above median education and financial literacy --------------
	*Summarizing education to create dummy variable for being above median in either
	sum educationYears, d
	gen abovep50FinLit = ((educationYears >= `r(p50)' & educationYears != .) | ///
		(finlit_score >= 2 & finlit_score != .)) 
		
*-------------------------------------------------------------------------------
* IV.34. Dicom variable
*-------------------------------------------------------------------------------		
	gen inDicom2008 = (p15 == 1) if p15 != 9 & p15 != .
	gen inDicom2009 = (inDicom == 1) if inDicom != 9  & inDicom != .
	
*-------------------------------------------------------------------------------
* IV.35. Personal question responses
*-------------------------------------------------------------------------------		
	gen capableSave2008 = (p332 < 3) if p332!= 9 
	gen betterThanAvg2008 = (p335 <3) if p335!= 9
	gen isConsistentPlans2008 = (p336 <3) if p336!= 9
	
	gen capableSave2009 = (m3_2 < 3) if m3_2 != 9  & m3_2 != .
	gen betterThanAvg2009 = (m3_5 <3) if m3_5!= 9 & m3_5 != .
	gen isConsistentPlans2009 = (m3_6 <3) if m3_6!= 9 & m3_6 != .
	
*===============================================================================
* V. Winsorization 
*===============================================================================
* V.3.b Creates winsorized variables -------------------------------------------

	foreach x in formalSavings allSavings shortTermBorrowing ///
		stBorrowFamily stBorrowProviders stBorrowInstitutions ///
		totalLending businessLending familyLending longTermBorrowing ///
		formalLoan mortgage formalSavingsAd allSavingsAd allSavingsAdPOS fSavingsIncPerCap ///
		allSavingsIncPerCap stBorrowingIncPerCap stBorFamIncPerCap ///
		stBorProvIncPerCap stBorInstIncPerCap {
		
		if ("`x'" == "formalSavings" | "`x'" == "allSavings" | ///
		   "`x'" == "formalSavingsAd" | "`x'" == "allSavingsAd" | "`x'" == "allSavingsAdPOS" | ///
			"`x'" == "shortTermBorrowing" | ///
			"`x'" == "longTermBorrowing") {
			winsor `x'2008, gen(w10_`x'2008) p(0.1) highonly
			winsor `x'2009, gen(w10_`x'2009) p(0.1) highonly
		}	
		winsor `x'2008, gen(w5_`x'2008) p(0.05) highonly
		winsor `x'2009, gen(w5_`x'2009) p(0.05) highonly
		winsor `x'2008, gen(w1_`x'2008) p(0.01) highonly
		winsor `x'2009, gen(w1_`x'2009) p(0.01) highonly
		
	}
	
	foreach x in netSavings changeTotalWealth netSavingsAd changeTotalWealthAd {
		winsor `x'2008, gen(w10_`x'2008) p(0.1) 
		winsor `x'2009, gen(w10_`x'2009) p(0.1) 
		winsor `x'2008, gen(w5_`x'2008) p(0.05) 
		winsor `x'2009, gen(w5_`x'2009) p(0.05) 
		winsor `x'2008, gen(w1_`x'2008) p(0.01)
		winsor `x'2009, gen(w1_`x'2009) p(0.01) 
	}

*===============================================================================
* VI. Clean, labels, structures, saves main database				
*===============================================================================		
*-------------------------------------------------------------------------------
* V.1. Keep only relevant variables
*-------------------------------------------------------------------------------

	keep idBaseSurvey idFollowUpSurvey age educationYears incomePerCapita ///
		householdSize* hasPrevAccount accountAccess groupId ///
		formalSavings* inSample activeUser openedAccount ///
		numDepositsJul08Aug09 totalDepositedJul08Aug09 ///
		numWithdrawalsJul08Aug09 totalWithdrawalJul08Aug09 ///
		tDm* tWm* ///
		avgMonthSavingsAug08Aug09 shortTermBorrowing* ///
		stBorrowFamily* stBorrowProviders* stBorrowInstitutions* ///
		stBorrowTotalIndex* stBorrowFamilyIndex* ///
		stBorrowProvidersIndex* stBorrowInstitutionsIndex* ///
		totalLending* totalLendingIndex* familyLending* ///
		familyLendingIndex* businessLending* businessLendingIndex* ///
		regretsNotSaving allSavings* mealsCutback* meatCutback* ///
		medicinesCutback* schoolSuppliesCutback* clothingCutback* ///
		schoolSnacksCutback* transportCutback* eatingOutCutback* ///
		economicShock* anxietyFuture* economicDifficulty* ///
		female childrenAtHome difficultySurvey satisfactionFE ///
		peerGroupTreatment consumptionCutbacks* hadFormalSavings2008 ///
		headHousehold conflictsMoney sociallyTaxed fearStolen ///
		hyperbolicPreferences lentFamily owesFamily ipw* w5* w1* w10* ///
		d1_am* d1_bm* d1_cm* d1_dm* d1_em* d1_fm* d1_gm* d1_hm* ///
		d1_im* numberConsumptionCutbacks* intimidatedBank2008 ///
		longTermBorrowing* sdMonthSavingsAug08Aug09 ///
		highInterestTreatment longTermBorrowingIndex* jobLoss* ///
		bizDown* netSavings* netSavingsAd* changeTotalWealth* changeTotalWealthAd* d12 expDecision2008 ///
		savingDecision2008 hideSavingsFamily2008 hideSavingsSO2008 ///
		borrowSO2008 troubleSO2008 expDecision2009 ///
		savingDecision2009 hideSavingsFamily2009 hideSavingsSO2009 ///
		borrowSO2009 troubleSO2009 boughtElectronics* ///
		madeInvestment* improveHome* prePayFE* housingSubsidy* ///
		numberBulkyExp* anyBulkyExp* giftGiving* ///
		w1_formalSavingsAd* w1_allSavingsAd* allSavingsAdPOS* formalSavingsAd* ///
		w5_allSavingsAd* w10_allSavingsAd* w10_formalSavingsAd* w5_netSavingsAd* ///
		w5_changeTotalWealthAd* w10_netSavingsAd* w10_changeTotalWealthAd* w1_netSavingsAd*  ///
		w1_changeTotalWealthAd* formalSavingsAd* allSavingsAd* noPrevAccount2008 ///
		w5_formalSavingsAd* noFormalSavings* savingsCat* ///
		depositFE2009 depositHousingAcct2009 depositOtherAcct2009 ///
		withdrawFE2009 withdrawHousingAcct2009 withdrawOtherAcct2009 ///
		anyDeposit2009 anyWithdrawal2009 numberDeposits2009 ///
		numberWithdrawals2009 depositFEAcct2009 withdrawFEAcct2009 ///
		anyDepositAcct2009 anyWithdrawalAcct2009 numberDepositsAcct2009 ///
		numberWithdrawalsAcct2009 savingsFCat* hhIncome* d1_a* ///
		savingsFCatAd* noPrevAccountOnly2008 noPrevAccountOnlyM2008 ///
		selfReportedIncome* incomePerCap* lnIncomePerCap* ///
		fSavingsIncPerCap* allSavingsIncPerCap* stBorrowingIncPerCap* ///
		stBorFamIncPerCap* stBorProvIncPerCap* stBorInstIncPerCap* ///
		w5_fSavingsIncPerCap* w5_allSavingsIncPerCap* ///
		w5_stBorrowingIncPerCap* w5_stBorFamIncPerCap* ///
		w5_stBorProvIncPerCap* w5_stBorInstIncPerCap* borrow* iBorrow* prborrow*  ///
		prShortTermBorrowingTot* prShortTermBorrowFamily* prShortTermBorrowProv* ///
		prShortTermBorrowInst* prlongTermBorrowing* finalBalstartAug09* Gr0Aug09* ///
		prLendingTot* prLendingfamily* prLendingbusiness*  ///
		lnHHIncome* abovep50FinLit binIntimBank* inDicom2008 inDicom2009 ///
		capableSave* betterThanAvg* isConsistentPlans* otherAccounts* e2 transfers_3months* ecShm* ///
		jobShm* medShm* birShm*  pplShm* sepShm* robShm*  bizShm* marShm* detShm*
		
	order idBaseSurvey idFollowUpSurvey accountAccess inSample
	
* Creating wide form of data for ANCOVA analysis  

     foreach t in 2008 2009 {
      foreach x in shortTermBorrowing`t' stBorrowFamily`t' stBorrowProviders`t' ///
		stBorrowInstitutions`t' formalSavings`t' allSavings`t' ///
		longTermBorrowing`t' netSavings`t' changeTotalWealth`t' netSavingsAd`t' changeTotalWealthAd`t' ///
		allSavingsAd`t' formalSavingsAd`t' fSavingsIncPerCap`t' ///
		allSavingsIncPerCap`t' stBorrowingIncPerCap`t' ///
		stBorFamIncPerCap`t' stBorProvIncPerCap`t' stBorInstIncPerCap`t' ///
		totalLending`t' familyLending`t' businessLending`t' {
		
		gen IHS`x' = ln(`x' + sqrt(1+`x'^2)) 
		gen Ln`x' = ln(`x') 
		gen Ln1`x' = ln(`x' + 1) 
		if ("`x'" == "formalSavings`t'"      | "`x'" == "allSavings`t'"          | ///
		    "`x'" == "formalSavingsAd`t'"    | "`x'" == "allSavingsAd`t'"        | ///
		    "`x'" == "shortTermBorrowing`t'" | "`x'" == "longTermBorrowing`t'"   | ///
		    "`x'" == "netSavings`t'"         | "`x'" == "changeTotalWealth`t'"   | ///
		    "`x'" == "netSavingsAd`t'"       | "`x'" == "changeTotalWealthAd`t'" | ///
		    "`x'" == "totalLending`t'"       | "`x'" == "familyLending`t'"       | ///
		    "`x'" == "businessLending`t'") {
			gen Pr`x' = (`x' > 0) if `x' != .		
		}		
	}
}

	save $inputPath/impactDatabase_wide.dta, replace
	u $inputPath/impactDatabase_wide.dta, clear

*-------------------------------------------------------------------------------
* V.2. Converts wide form to long form
*-------------------------------------------------------------------------------
		
	reshape long formalSavings shortTermBorrowing stBorrowFamily ///
		stBorrowProviders stBorrowInstitutions allSavings ///
		stBorrowTotalIndex stBorrowFamilyIndex ///
		stBorrowProvidersIndex stBorrowInstitutionsIndex ///
		totalLending totalLendingIndex familyLending ///
		familyLendingIndex businessLending businessLendingIndex ///
		mealsCutback meatCutback medicinesCutback ///
		schoolSuppliesCutback clothingCutback economicShock ///
		schoolSnacksCutback transportCutback eatingOutCutback ///
		anxietyFuture economicDifficulty consumptionCutbacks ///
		anxietyFutureFirst anxietyFutureSecond anxietyFutureThird ///
		economicDifficultyFirst economicDifficultySecond ///
		economicDifficultyThird economicDifficultyFourth ///
		w1_formalSavings w1_allSavings w1_shortTermBorrowing ///
		w1_stBorrowFamily w1_stBorrowProviders w1_stBorrowInstitutions ///
		w1_totalLending w1_businessLending w1_familyLending ///
		w5_formalSavings w5_allSavings w5_shortTermBorrowing ///
		w5_stBorrowFamily w5_stBorrowProviders w5_stBorrowInstitutions ///
		w5_totalLending w5_businessLending w5_familyLending ///
		d1_am d1_bm d1_cm d1_dm d1_em d1_fm d1_gm d1_hm d1_im ///
		numberConsumptionCutbacks w10_allSavings w10_formalSavings ///
		longTermBorrowing w1_longTermBorrowing w5_longTermBorrowing ///
		w10_longTermBorrowing w10_shortTermBorrowing w5_formalLoan ///
		w5_mortgage longTermBorrowingIndex jobLoss bizDown ///
		netSavings changeTotalWealth netSavingsAd changeTotalWealthAd w1_changeTotalWealth w1_changeTotalWealthAd w5_changeTotalWealth w5_changeTotalWealthAd w10_changeTotalWealth  w10_changeTotalWealthAd ///
		w1_netSavings w1_netSavingsAd w5_netSavings w5_netSavingsAd w10_netSavings w10_netSavingsAd expDecision ///
		savingDecision hideSavingsFamily hideSavingsSO borrowSO ///
		troubleSO boughtElectronics madeInvestment improveHome ///
		prePayFE housingSubsidy numberBulkyExp anyBulkyExp ///
		giftGiving w1_formalSavingsAd w1_allSavingsAd ///
		formalSavingsAd w5_allSavingsAd w10_allSavingsAd ///
		w10_formalSavingsAd noPrevAccount noFormalSavings ///
		allSavingsAd allSavingsAdPOS w5_formalSavingsAd hhIncome householdSize ///
		depositFE depositHousingAcct depositOtherAcct withdrawFE ///
		withdrawHousingAcct withdrawOtherAcct anyDeposit /// 
		anyWithdrawal numberDeposits numberWithdrawals  ///
		depositFEAcct withdrawFEAcct anyDepositAcct anyWithdrawalAcct ///
		numberDepositsAcct numberWithdrawalsAcct savingsCat ///
		savingsCatAd savingsFCat savingsFCatAd ///
		noPrevAccountOnly noPrevAccountOnlyM  lnIncomePerCapA ///
		selfReportedIncome incomePerCap ///
		lnIncomePerCap fSavingsIncPerCap allSavingsIncPerCap ///
		stBorrowingIncPerCap stBorFamIncPerCap stBorProvIncPerCap ///
		stBorInstIncPerCap w5_fSavingsIncPerCap w5_allSavingsIncPerCap ///
		w5_stBorrowingIncPerCap w5_stBorFamIncPerCap w5_stBorProvIncPerCap ///
		w5_stBorInstIncPerCap borrowFinancieras borrowCooperatives ///
		borrowMoneyLenders borrowParents borrowSonsDaughters ///
		borrowSiblings borrowPartner borrowFriends borrowRelatives ///
		iborrowParents iborrowSonsDaughters ///
		iborrowSiblings iborrowPartner iborrowFriends iborrowRelatives ///
		borrowSuppliers borrowBusContacts borrowHealth ///
		borrowSchools borrowServProv borrowRegularClient ///
		prborrowParents prborrowSonsDaughters ///
		prborrowSiblings prborrowPartner prborrowFriends prborrowRelatives ///
		prlongTermBorrowing ///
		iBorrowFinancieras iBorrowCooperatives iBorrowMoneyLenders ///
		iBorrowParents iBorrowSonsDaughters iBorrowSiblings iBorrowPartner ///
		iBorrowFriends iBorrowRelatives iBorrowSuppliers ///
		iBorrowBusContacts iBorrowHealth iBorrowSchools iBorrowServProv ///
		iBorrowRegularClient prShortTermBorrowingTot prShortTermBorrowFamily prShortTermBorrowProv ///
		prShortTermBorrowInst lnHHIncome ///
		prLendingTot prLendingfamily prLendingbusiness ///
		formalSavingsAdi allSavingsAdi binIntimBank inDicom ///
		capableSave betterThanAvg isConsistentPlans otherAccounts transfers_3months ///
		, i(idBaseSurvey) j(year) 
			
	xtset idBaseSurvey year, yearly

*-------------------------------------------------------------------------------
* V.3. Creates additional key variables
*-------------------------------------------------------------------------------
* V.3.a Creates "Post" dummy ---------------------------------------------------

	gen post = (year == 2009)
	gen attritor = (inSample == 0)
	
	gen t1 = 1 if economicShock == 1 & post == 1
	egen economicShock2009 = max(t1), by(idBaseSurvey) 
	gen t2 = 1 if economicShock == 0 & post == 1
	egen t3 = max(t2), by(idBaseSurvey) 
	replace economicShock2009 = 0 if t3 == 1
	drop t1 t2 t3

	
	gen t1 = 1 if economicShock == 1 & post == 0
	egen economicShock2008 = max(t1), by(idBaseSurvey) 
	gen t2 = 1 if economicShock == 0 & post == 0
	egen t3 = max(t2), by(idBaseSurvey) 
	replace economicShock2008 = 0 if t3 == 1
	drop t1 t2 t3

* V.4.c Creates other functional form variables --------------------------------

	foreach x in shortTermBorrowing stBorrowFamily stBorrowProviders ///
		stBorrowInstitutions formalSavings allSavings ///
		longTermBorrowing netSavings changeTotalWealth netSavingsAd changeTotalWealthAd ///
		allSavingsAd allSavingsAdPOS formalSavingsAd fSavingsIncPerCap ///
		allSavingsIncPerCap stBorrowingIncPerCap ///
		stBorFamIncPerCap stBorProvIncPerCap stBorInstIncPerCap ///
		totalLending familyLending businessLending {
		
		gen IHS`x' = ln(`x' + sqrt(1+`x'^2))
		gen Ln`x' = ln(`x') 
		gen Ln1`x' = ln(`x' + 1) 
		if ("`x'" == "formalSavings" | "`x'" == "allSavings"| ///
		   "`x'" == "formalSavingsAd" | "`x'" == "allSavingsAd"| "`x'" == "allSavingsAdPOS"| ///
			"`x'" == "shortTermBorrowing" | ///
			 "`x'" == "longTermBorrowing" | ///
			"`x'" == "netSavings" | "`x'" == "changeTotalWealth" | ///
			"`x'" == "netSavingsAd" | "`x'" == "changeTotalWealthAd") {
			gen Pr`x' = (`x' > 0) if `x' != .
		}		
	}

	*Probability for more variables:
	foreach x in fSavingsIncPerCap allSavingsIncPerCap stBorrowingIncPerCap ///
		stBorFamIncPerCap stBorProvIncPerCap stBorInstIncPerCap {
		gen Pr`x' = (`x' > 0) if `x' != .
		}
	
* V.4.c Normalizes variables ---------------------------------------------------
	
	*Saving variable without normalizing for other in-text analysis 
	gen anxietyFutureAnswer =  anxietyFuture
	gen economicDifficultyAnswer = economicDifficulty
	
	*Now normalizing:	
	foreach y in anxietyFuture economicDifficulty { 
		
		sum `y' if post==0
		replace `y' = (`y' - `r(mean)') / `r(sd)'
	} 
	
*-------------------------------------------------------------------------------
* V.4. Labeling
*-------------------------------------------------------------------------------
	
	label var female "Female"
	label var age "Age"
	label var educationYears "Years of education"
	label var childrenAtHome "Children at home"
	label var lnIncomePerCap "ln(Income per capita)"
	label var hasPrevAccount "Has prior savings account"
	label var headHousehold "Head of household"
	label var conflictsMoney "Conflicts with partner over money"
	label var sociallyTaxed "Socially taxed"
	label var regretsNotSaving "Regrets not saving more"
	label var hyperbolicPreferences "Hyperbolic preferences"
	label var fearStolen "Fear savings stolen in the home"
	label var lentFamily "Lent to family or friends"
	label var owesFamily "Owes to family or friends"
	label var binIntimBank "Is intimidated of going to the bank"
	
	foreach x in formalSavings allSavings shortTermBorrowing ///
		stBorrowFamily stBorrowProviders stBorrowInstitutions ///
		totalLending businessLending familyLending {
		
		label var w5_`x' "Top 5\%"
		label var w1_`x' "Top 1\%"
		capture label var w10_`x' "Top 10\%"
		
	}

	foreach x in fSavingsIncPerCap allSavingsIncPerCap stBorrowingIncPerCap ///
		stBorFamIncPerCap stBorProvIncPerCap stBorInstIncPerCap {
		
		label var w5_`x' "Top 5\%"
		
	}	
	
	foreach x in formalSavings allSavings shortTermBorrowing ///
		stBorrowFamily stBorrowProviders stBorrowInstitutions ///
		totalLending businessLending familyLending netSavings changeTotalWealth {
				
		label var w5_`x' "Top \& Bottom 5\%"
		label var w1_`x' "Top \& Bottom 1\%"
		capture label var w10_`x' "Top \& Bottom 10\%"
		
	}
*-------------------------------------------------------------------------------
* V.5. Saves database
*-------------------------------------------------------------------------------
	compress
	save $inputPath/impactDatabase.dta, replace

*-------------------------------------------------------------------------------
* V.6. Merge asesor data and save again.
*-------------------------------------------------------------------------------
*Merge asesor dta for stratification at the level of randomization 
	merge m:1 idBaseSurvey using $inputPath/asesor2008.dta
	//all 8350 initial sample matched, drop _merge == 2
	drop if _merge ==2
	drop _merge
	sort idBaseSurvey year

	save $inputPath/impactDatabase.dta, replace
*===============================================================================
* END
*===============================================================================
